$(function() {
	
	var mid = $("#mid").val();
	console.log(mid)
	var assessOpe = new AssessOperation();
	// 加flag

	$.post("/meat/sec/assess/count",
			{
				"mid" : mid
			},
			function(data) {
				console.log(data.data)
				 $("#rating-input").rating({
			         min: 0,
			         max: 5,
			         step: 0.1,
			         size: 'sm',
			         showClear: false,
			     });
			}
	);
	
	
	 
	 
	 
	var pageObj = {

		max : 5,
		page : 1,
		data : mid
	};

	assessOpe.findAllAsserts(pageObj, false);
	
	$("#oldAss").on("click",function(){
		
		assessOpe.findAllAsserts(pageObj, true);
	})
	$("#newAss").on("click",function(){
		
		assessOpe.findAllAsserts(pageObj, false);
	})

});

// 对 merchant的操作
function AssessOperation(mid) {

	this.mid=mid;
	this.model = null;
	this.template = null;

	this.findAllAsserts = function(po, replyFlag, mid) {

		var url;
		if (replyFlag == true) {
			url = "/meat/sec/assess/replylist";
		} else {
			url = "/meat/sec/assess/noreplylist";
		}

		aj.ajax("POST", url, po, {
			"content-Type" : "application/json"
		}, this, function(callback) {

			console.log(callback.data)
			this.model = callback.data;
			if (replyFlag) {
				template = $("#panelOne");// has reply
				_renderReply(callback.data);
			} else {
				template = $("#panelTwo");// not reply yet
				_renderNoReply(callback.data, po);
			}
			// this.model = customers;
			// _render(this.model);
		});
	};

	function _renderReply(data) {

		$("#accordion").empty();
		data.forEach(function(item) {
			var id = item.id;
			console.log(id)
			template.clone(true).css("display", "block").find(
					"span[name=cname]").text(item.cname).end().find(
					"span[name=score]").text(item.score+" stars").end().find(
					"span[name=assesstime]").text(
					formatDateTime(item.assertTime)).end().find(
					"span[name=assess1]").text(item.msg).end().find(
					"span[name=mname]").text(item.mname + ":").end().find(
					"span[name=reply]").text(item.reply).end().find(
					"div[id=panelHead]").attr("href", "#" + id).end().find(
					"div[id=collapseOne]").attr("id", id).end().find(
					"span[name=replytime]")
					.text(formatDateTime(item.replyTime)).end().find(
							"div[id=panelOne]").attr("id", id).end().appendTo(
							$("#accordion"));
			$("#" + id).collapse('hide');
		});
	}

	function _renderNoReply(data, po) {

		$("#accordion").empty();
		data.forEach(function(item) {
			var id = item.id;
			console.log(id)
			template.clone(true).css("display", "block").find(
					"span[name=cname]").text(item.cname).end().find(
					"span[name=score]").text(item.score+" stars").end().find(
					"span[name=assesstime]").text(
					formatDateTime(item.assertTime)).end().find(
					"span[name=assess2]").text(item.msg).end().find(
					"div[id=panelHead]").attr("href", "#" + id).attr("assid",
					id).end().find("div[id=collapseOne]").attr("id", id).end()
					.find("#panelTwo").attr("assessid", item.id).end()
					.appendTo($("#accordion"));
		})

		$("div[id=panelTwo]").on("click", function(e) {

			var assid = $(this).children(":first").attr('assid');
			console.log(assid)
			swal({
				title : "Reply to customer",
				text : "Input your reply",
				type : "input",
				showCancelButton : true,
				closeOnConfirm : false,
				animation : "slide-from-top",
				inputPlaceholder : "Write something"
			}, function(inputValue) {
				if (inputValue === false)
					return false;
				if (inputValue === "") {
					swal.showInputError("You need to write something!");
					return false
				}
				var reply = {

					id : assid,
					reply : inputValue
				}
				aj.ajax("PATCH", "/meat/sec/assess/reply", reply, {
					"content-Type" : "application/json"
				}, this, function(callback) {

					aj.assessOpe.findAllAsserts(po, false);
					swal("Done!", callback.msg, "success");
				});
			});
		});
	}
	// $("#panelOne").css("display", "none")
};

function formatDateTime(inputTime) {
	var date = new Date(inputTime);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	return y + '-' + m + '-' + d;
};

(function() {// 一次函数
	var obj = {};

	// 封装Ajax
	obj.ajax = function(type, url, data, headers, context, callback) {

		$.ajax({
			url : url,
			type : type,
			data : JSON.stringify(data),
			headers : headers,
			context : context
		// 回调函数的this
		}).done(callback);
	};

	obj.assessOpe = new AssessOperation();
	window.aj = obj;

})();